#include "diskio.h"
#include "sdio_sd.h"
#include "protocol.h"

/*-----------------------------------------------------------------------*/
/* Correspondence between physical drive number and physical drive.      */

#define ATA		0
#define MMC		1
#define USB		2
#define SECTOR_SIZE 	512 /* Block Size in Bytes */



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
DSTATUS disk_initialize (
	BYTE drv				/* Physical drive nmuber (0..) */
)
{
//	if (SD_Init() == SD_OK)
	  return 0;//STA_OK; 	
//	return STA_NOINIT;
}
/*-----------------------------------------------------------------------*/
/* Return Disk Status                                                    */

DSTATUS disk_status (
	BYTE drv		/* Physical drive nmuber (0..) */
)
{
	if(drv)
	{
		return STA_NOINIT;
	}	
	return 0;
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */

DRESULT disk_read (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE *buff,		/* Data buffer to store read data */
	DWORD sector,	/* Sector address (LBA) */
	BYTE count		/* Number of sectors to read (1..255) */
)
{
	SD_Error res = SD_OK;

	if (drv || !count)
            return RES_PARERR;  //????????,count????0,????????
	if(count==1)
	{
          res = SD_ReadBlock(buff,sector << 9  ,SECTOR_SIZE);
          SD_WaitReadOperation();
          while(SD_GetStatus() != SD_TRANSFER_OK);
        }
	else
	{
          res =  SD_ReadMultiBlocks(buff,sector << 9 ,SECTOR_SIZE,count);
          SD_WaitReadOperation();
          while(SD_GetStatus() != SD_TRANSFER_OK);
        }
	if(res == SD_OK)
          return RES_OK;
        else
          return RES_ERROR;
	//return RES_PARERR;
}



/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */

#if _READONLY == 0
DRESULT disk_write (
	BYTE drv,			/* Physical drive nmuber (0..) */
	const BYTE *buff,	/* Data to be written */
	DWORD sector,		/* Sector address (LBA) */
	BYTE count			/* Number of sectors to write (1..255) */
)
{
	SD_Error res = SD_OK;

        if (drv || !count)
          return RES_PARERR;  
	if(count==1)          
    	{
          res =SD_WriteBlock((u8 *)buff,sector << 9 ,SECTOR_SIZE);
          SD_WaitReadOperation();
          while(SD_GetStatus() != SD_TRANSFER_OK);
        }
	else
        {
    	  res = SD_WriteMultiBlocks((u8 *)buff,sector <<9 ,SECTOR_SIZE,count);
          SD_WaitReadOperation();
          while(SD_GetStatus() != SD_TRANSFER_OK);
        }
  	if(res == SD_OK)
        {

          return RES_OK;
        }
        else
        return RES_ERROR;
}
#endif /* _READONLY */
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */

DRESULT disk_ioctl (
	BYTE drv,		/* Physical drive nmuber (0..) */
	BYTE ctrl,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	return RES_OK;
}

DWORD get_fattime(void)
{
    return RTC_GetCounter();
}

